<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="attendanceGroupName">
        <el-input
          v-model="queryParams.attendanceGroupName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考勤规则" prop="attendanceRuleId">
        <el-select
          v-model="queryParams.attendanceRuleId"
          placeholder="请选择考勤规则"
          clearable
        >
          <el-option
            v-for="dict in attendanceRuleIdOptions"
            :key="dict.attendanceRuleId"
            :label="dict.attendanceName"
            :value="dict.attendanceRuleId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ehr:attend_group:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ehr:attend_group:edit']"
          >修改</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleAttendDept"
          >设置考勤部门</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-user"
          size="mini"
          :disabled="multiple"
          @click="handleAttendPerson"
          >设置考勤人员</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="attend_groupList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="考勤组ID"
        align="center"
        prop="attendanceGroupId"
      />
      <el-table-column label="名称" align="center" prop="attendanceGroupName" />
      <el-table-column
        label="考勤规则"
        align="center"
        prop="attendanceRuleId"
        width="180"
        :formatter="attendanceRuleIdFormat"
      />
      <el-table-column
        label="考勤地址"
        align="center"
        prop="attendanceAddId"
        width="180"
        :formatter="attendanceAddIdFormat"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="statusId"
        width="180"
        :formatter="statusIdFormat"
      />
      <el-table-column
        label="最后更新时间"
        align="center"
        prop="lastUpdatedStamp"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.lastUpdatedStamp, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:attend_group:detail']"
          >详情</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:attend_group:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:attend_group:remove']"
            >删除</el-button
          >
          <el-dropdown
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
            v-hasPermi="['ehr:attend_group:edit']"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleDept"
                icon="el-icon-circle-check"
                v-hasPermi="['system:role:edit']"
                >设置考勤部门</el-dropdown-item
              >
              <el-dropdown-item
                command="handleEmpl"
                icon="el-icon-user"
                v-hasPermi="['system:role:edit']"
                >设置考勤人员</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDept(scope.row)" 
          >设置考勤部门</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleEmpl(scope.row)"
          >设置考勤人员</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考勤组设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        v-bind:disabled="detailFlag"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="attendanceGroupName">
              <el-input
                v-model="form.attendanceGroupName"
                placeholder="请输入名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤规则" prop="attendanceRuleId">
              <el-select
                v-model="form.attendanceRuleId"
                placeholder="请选择考勤规则"
              >
                <el-option
                  v-for="dict in attendanceRuleIdOptions"
                  :key="dict.attendanceRuleId"
                  :label="dict.attendanceName"
                  :value="dict.attendanceRuleId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考勤地址" prop="attendanceAddId">
              <el-select
                v-model="form.attendanceAddId"
                placeholder="请选择考勤地址"
              >
                <el-option
                  v-for="dict in attendanceAddIdOptions"
                  :key="dict.addressId"
                  :label="dict.addressName"
                  :value="dict.addressId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤方式" prop="attendanceFromTypeId">
              <el-select
                v-model="form.attendanceFromTypeId"
                placeholder="请选择考勤方式"
              >
                <el-option
                  v-for="dict in attendanceFromTypeIdOptions"
                  :key="dict.enumCode"
                  :label="dict.description"
                  :value="dict.enumCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生效开始" prop="fromDate">
              <el-date-picker
                clearable
                v-model="form.fromDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00"
                placeholder="请选择生效开始"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生效结束" prop="thruDate">
              <el-date-picker
                clearable
                v-model="form.thruDate"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                default-time="00:00:00"
                placeholder="请选择生效结束"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="statusId">
              <el-select v-model="form.statusId" placeholder="请选择状态">
                <el-option
                  v-for="dict in statusIdOptions"
                  :key="dict.enumCode"
                  :label="dict.description"
                  :value="dict.enumCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAttend_group,
  getAttend_group,
  delAttend_group,
  addAttend_group,
  updateAttend_group,
} from "@/api/ehr/attend_group";

export default {
  name: "Attend_group",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selcet_row_form: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤组设置表格数据
      attend_groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 考勤规则业务字典
      attendanceRuleIdOptions: [],
      // 考勤地址业务字典
      attendanceAddIdOptions: [],
      // 考勤方式业务字典
      attendanceFromTypeIdOptions: [],
      // 状态业务字典
      statusIdOptions: [],

      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        attendanceGroupName: null,
        attendanceRuleId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attendanceGroupName: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 考勤规则业务字典
    this.getEntityDicts("AttendanceRule:attendanceRuleId:attendanceName").then(
      (response) => {
        this.attendanceRuleIdOptions = response.data;
      }
    );
    // 考勤地址业务字典
    this.getEntityDicts("AttendanceAddress:addressId:addressName").then(
      (response) => {
        this.attendanceAddIdOptions = response.data;
      }
    );
    // 考勤方式业务字典
    this.getEnumDicts("ACTIVE_STOP").then((response) => {
      this.attendanceFromTypeIdOptions = response.data;
    });
    // 状态业务字典
    this.getEnumDicts("ACTIVE_STOP").then((response) => {
      this.statusIdOptions = response.data;
    });
    this.getList();
  },
  methods: {
    attendanceRuleIdFormat(row, column) {
      let val = null;
      this.attendanceRuleIdOptions.forEach((item, index) => {
        if (row.attendanceRuleId === item.attendanceRuleId) {
          val = item.attendanceName;
        }
      });
      return val;
    },
    attendanceAddIdFormat(row, column) {
      let val = null;
      this.attendanceAddIdOptions.forEach((item, index) => {
        if (row.attendanceAddId === item.addressId) {
          val = item.addressName;
        }
      });
      return val;
    },
    attendanceFromTypeIdFormat(row, column) {
      let val = null;
      this.attendanceFromTypeIdOptions.forEach((item, index) => {
        if (row.attendanceFromTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    statusIdFormat(row, column) {
      let val = null;
      this.statusIdOptions.forEach((item, index) => {
        if (row.statusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询考勤组设置列表 */
    getList() {
      this.loading = true;
      listAttend_group(this.queryParams).then((response) => {
        this.attend_groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attendanceGroupId: null,
        attendanceGroupName: null,
        attendanceRuleId: null,
        attendanceAddId: null,
        attendanceFromTypeId: null,
        fromDate: null,
        thruDate: null,
        statusId: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.attendanceGroupId);
      selection.map((item) => (this.selcet_row_form = item));
      console.info("select-form:" + this.selcet_row_form);

      console.info("ids:" + this.ids);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDept":
          this.handleDept(row);
          break;
        case "handleEmpl":
          this.handleEmpl(row);
          break;
        default:
          break;
      }
    },
    /** 设置考勤组的考勤部门 */
    handleAttendDept(row) {
      const partyId = row.partyId || this.ids;
      this.selcet_row_form.setAttendDept = true;
      var _path = "/attend-group/set-dept";
      this.$router.push({
        path: _path,
        query: {
          form: JSON.stringify(this.selcet_row_form),
        },
      });
    },
    /** 设置考勤组的考勤人员 */
    handleAttendPerson(row) {
      const partyId = row.partyId || this.ids;
      this.selcet_row_form.setAttendDept = false;
      var _path = "/attend-group/set-person";
      this.$router.push({
        path: _path,
        query: {
          form: JSON.stringify(this.selcet_row_form),
        },
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      (this.editFlag = true), (this.updateFlag = false);
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加考勤组设置";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      (this.editFlag = false), (this.updateFlag = false);
      this.detailFlag = true;
      this.reset();
      const attendanceGroupId = row.attendanceGroupId || this.ids;
      getAttend_group(attendanceGroupId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "考勤组设置详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const attendanceGroupId = row.attendanceGroupId || this.ids;
      getAttend_group(attendanceGroupId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤组设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.attendanceGroupId != null && this.updateFlag) {
            updateAttend_group(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttend_group(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attendanceGroupIds = row.attendanceGroupId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除考勤组设置编号为"' + attendanceGroupIds + '"的数据项？'
        )
        .then(function () {
          return delAttend_group(attendanceGroupIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ehr/attend_group/export",
        {
          ...this.queryParams,
        },
        `attend_group_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
